local BuildHelper = {}

function BuildHelper.buildSkill(config, entity)
    local skill = require("gameLogic.fightSkill.show." .. config.show_name).new(config)
    return skill
end

function BuildHelper.buildAttrByFightAttr(config)
    local attr = require("gameLogic.fightEntity.Attributes").new()
    local attribute = require("config.attribute")
    local length = attribute.length()
    for i = 1, length do
        local data = attribute.indexOf(i)
        attr:setAttrValue(data.id, config["attr_" .. data.id] or 0, true)
    end
    return attr
end

function BuildHelper.buildHurtStrategy(config, skill, element)
    local AttributeConst = require("const.AttributeConst")
    local baseStrategy = nil
    if element == AttributeConst.ELEMENT_PHYSICS then
        baseStrategy = require("gameLogic.fight.fightHurtStrategy.HurtPhysics").new(config, skill, element)
    elseif element == AttributeConst.ELEMENT_FIRE then
        baseStrategy = require("gameLogic.fight.fightHurtStrategy.HurtElement").new(config, skill, element)
    elseif element == AttributeConst.ELEMENT_WATER then
        baseStrategy = require("gameLogic.fight.fightHurtStrategy.HurtElement").new(config, skill, element)
    elseif element == AttributeConst.ELEMENT_THUNDER then
        baseStrategy = require("gameLogic.fight.fightHurtStrategy.HurtElement").new(config, skill, element)
    elseif element == AttributeConst.ELEMENT_TREATMENT then
        baseStrategy = require("gameLogic.fight.fightHurtStrategy.HurtTreatment").new(config, skill, element)
    end
    assert(baseStrategy, "can not create trategy by element=" .. element)
    if config.suck_blood > 0 then
        local fianlStrategy =
            require("gameLogic.fight.fightHurtStrategy.HurtSuckBlood").new(config, skill, baseStrategy)
        return fianlStrategy
    else
        return baseStrategy
    end
end

function BuildHelper.buildBuff(buffId, entity, whoEntity)
    local config = require("config.buff").get(buffId)
    assert(config, "can not found cfg in buff by id=" .. buffId)
    -- if config.class == "BuffShareHurt" then

    if config.class == "BuffResistHurt" then
        return require("gameLogic.fight.fightBuff." .. config.class).new(config, whoEntity, whoEntity, entity)
    else
        return require("gameLogic.fight.fightBuff." .. config.class).new(config, entity, whoEntity)
    end
end

function BuildHelper.buildEntity(excuter, camp, col, row, id, heroUnit)
    local AttrDataHelper = require("data.helper.AttrDataHelper")
    local FightEffectHelper = require("gameLogic.fight.fightEffect.FightEffectHelper")
    local skill_ = require("config.fight_skill")
    local config = heroUnit:getConfig()
    local normalConfig = skill_.get(config.skill_normal)
    assert(normalConfig, "can not found cfg in fight_skill by id= " .. config.skill_normal)
    local entity =
        require("gameLogic.fight.fightEntity.Entity").new(excuter, normalConfig, id, heroUnit:getConfig().element)
    for i = 1, 3 do
        local skillId = config["skill_id_" .. i]
        if skillId > 0 then
            local config = skill_.get(skillId)
            assert(config, "can not found cfg in fight_skill by id=" .. skillId)
            entity:addSkill(config)
        end
    end
    entity:setDebugName(heroUnit:getConfig().name .. "[id=" .. id .. "]")
    local beha = require("gameLogic.fight.fightBehavior.WaitBehavior").new(SignalConst.EVENT_RECEIVE_USER_SKILL)
    entity:setBehavior(beha)
    entity:setCamp(camp)
    entity:setStanding({col = col, row = row})
    local allAttrs = AttrDataHelper.getAllAttr(heroUnit)
    entity:initFightAttr(allAttrs)
    excuter:addEntity(entity)
    local effects = FightEffectHelper.getFightEffects(heroUnit)
    excuter:initFightEffect(effects, entity)
    entity:setFeature(BuildHelper.getPeopleFeature(config))
    return entity
end

function BuildHelper.getPeopleFeature(config)
    local peo = {}
    peo.sex = config.sex
    peo.race = config.race
    return peo
end

function BuildHelper.buildMonsterEntity(excuter, camp, col, row, id, param)
    local AttrDataHelper = require("data.helper.AttrDataHelper")
    local monsterCfg = param.cfg
    local skill_ = require("config.fight_skill")
    local normalConfig = skill_.get(monsterCfg.skill_normal)
    assert(normalConfig)
    local entity = require("gameLogic.fight.fightEntity.Entity").new(excuter, normalConfig, id, (monsterCfg.element))
    for i = 1, 3 do
        local skillId = monsterCfg["skill_id_" .. i]
        if skillId > 0 then
            local config = skill_.get(skillId)
            assert(config, "can not found cfg in fight_skill by id=" .. skillId)
            entity:addSkill(config)
        end
    end
    entity:setDebugName(monsterCfg.name .. "[id=" .. id .. "]")
    local beha = require("gameLogic.fight.fightBehavior.AutoBehavior").new()
    entity:setBehavior(beha)
    entity:setCamp(camp)
    entity:setStanding({col = col, row = row})
    local allAttrs = AttrDataHelper.getMonsterAttr(monsterCfg.id, param.level)
    entity:initFightAttr(allAttrs)
    entity:setFeature(BuildHelper.getPeopleFeature(monsterCfg))
    excuter:addEntity(entity)
end

function BuildHelper.getShowSkillCfg(skillId)
    local skillCfg = require("config.fight_skill").get(skillId)
    assert(skillCfg, "can not found cfg in fight_skill by id=" .. skillId)
    local skillShowCfg = require("config.skill_res").get(skillCfg.skill_res)
    return skillShowCfg, skillCfg.name
end

return BuildHelper
